layui.use(['table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 盘点计划表管理
     */
    var PandianJihua = {
        tableId: "pandianJihuaTable"
    };

    /**
     * 盘点人员表管理
     */
    var PandianJihuaUser = {
        tableId: "pandianJihuaUserTable"
    };

    /**
     * 初始化表格的列
     */
    PandianJihua.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'status', sort: true, title: '状态',
            	templet: function(d){
            		var str = "";
            		if (d.status == '未开始') {
                    	str += '<span style="font-weight: bold;">' + d.status + '</span>'
					} else if (d.status == '进行中'){
                    	str += '<span style="color:#ffaa00;font-weight: bold;">' + d.status + '</span>'
					} else if (d.status == '已结束'){
                    	str += '<span style="color:#00c300;font-weight: bold;">' + d.status + '</span>'
					} 
            		return str
            	}
			},
            {field: 'diskName', sort: true, title: '盘点名称', style:'font-weight: 600;'},
            {field: 'jihuaTime', sort: true, title: '计划时间', style:'font-weight: 600;'},
            {field: 'diskType', sort: true, title: '盘点类型', style:'font-weight: 600;'},
            {field: 'speed', sort: true, title: '进度%', style:'font-weight: 600;'},
            {field: 'countNum', sort: true, title: '开盘总库存', style:'font-weight: 600;'},
            {field: 'countVolume', sort: true, title: '开盘总体积m3', style:'font-weight: 600;'},
            {field: 'alreadyNum', sort: true, title: '已盘数量', style:'font-weight: 600;color:#ffaa00;'},
            {field: 'alreadyVolume', sort: true, title: '已盘体积', style:'font-weight: 600;color:#ffaa00;'},
            {field: 'warehouseName', sort: true, title: '仓库名称'},
            {field: 'createTime', sort: true, title: '创建时间'},
            {field: 'createUserName', sort: true, title: '创建用户'},
            {field: 'overTime', sort: true, title: '结束时间'},
            {field: 'overUserName', sort: true, title: '结束用户'},
            {align: 'center', toolbar: '#tableBar', title: '操作',fixed: 'right',width: 140}
        ]];
    };

    /**
     * 点击查询按钮
     */
    PandianJihua.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        queryData['warehouseId'] = $("#warehouseId").val();
        table.reload(PandianJihua.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    /**
     * 弹出添加对话框
     */
    PandianJihua.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/pandianJihua/add';
    };

    /**
     * 导出excel按钮
     */
    PandianJihua.exportExcel = function () {
        var checkRows = table.checkStatus(PandianJihua.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    PandianJihua.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/pandianJihua/edit?id=' + data.id;
    };

    /**
     * 点击结束
     */
    PandianJihua.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/pandianJihua/delete", function (data) {
	        	if (data.success) {
	                Feng.success("操作成功!");
	                table.reload(PandianJihua.tableId);
				} else {
		            Feng.error("操作失败！" + data.message)
				}
            }, function (data) {
                Feng.error("操作失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("确认结束吗?", operation);
    };

    /**
     * 点击开始
     */
    PandianJihua.onStartItem = function (data) {
        layer.confirm('确认开始吗?', function(index){
        	layer.close(index);
        	var msg;
            $.ajax({
            	url:Feng.ctxPath + "/pandianJihua/start?id="+data.id,
				data:{},
            	cache: false,
            	dataType:"json",
            	beforeSend:function(){
            		msg = layer.msg('提交中。。。请耐心等待', {
            			  icon: 16,
            			  shade: [0.2,'#000'], //0.1透明度的白色背景
            			  time: false
            		});
            	},
            	success:function(data){
                	layer.close(msg); 
            		if (data.success) {
		                Feng.success("操作成功!");
		                table.reload(PandianJihua.tableId);
		                table.reload(PandianJihuaUser.tableId);
					} else {
                    	Feng.error("转入失败!" + data.message + "!");
					}
            	},
            	error:function(data){
                	layer.close(msg); 
                    Feng.error("操作失败！" + data.responseJSON.message)
            	}
            })
        }); 
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + PandianJihua.tableId,
        url: Feng.ctxPath + '/pandianJihua/list',
        page: true,
        limit: 20,
        height: 390,
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: PandianJihua.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        PandianJihua.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        PandianJihua.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        PandianJihua.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + PandianJihua.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            PandianJihua.openEditDlg(data);
        } else if (layEvent === 'delete') {
            PandianJihua.onDeleteItem(data);
        } else if (layEvent === 'start') {
            PandianJihua.onStartItem(data);
        } 
    });

    /**
     * 初始化表格的列
     */
    PandianJihuaUser.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'diskName', sort: true, title: '盘点名称'},
            {field: 'areaName', sort: true, title: '库区名称'},
            {field: 'speed', sort: true, title: '进度%', style:'font-weight: 600;', totalRowText: '合计：'},
            {field: 'countNum', sort: true, title: '开盘总库存', style:'font-weight: 600;', totalRow: true},
            {field: 'countVolume', sort: true, title: '开盘总体积', style:'font-weight: 600;', totalRow: true},
            {field: 'alreadyNum', sort: true, title: '已盘数量', style:'font-weight: 600;color:#ffaa00;', totalRow: true},
            {field: 'alreadyVolume', sort: true, title: '已盘体积', style:'font-weight: 600;color:#ffaa00;', totalRow: true},
            {field: 'trueNum', sort: true, title: '实盘数量', style:'font-weight: 600;color:#2b85e4;', totalRow: true},
            {field: 'trueVolume', sort: true, title: '实盘体积', style:'font-weight: 600;color:#2b85e4;', totalRow: true},
            {field: 'userName', sort: true, title: '用户名称', style:'font-weight: 600;'},
            {field: 'userAccount', sort: true, title: '用户账号'},
            {align: 'center', toolbar: '#tableBarDetailed', title: '操作',fixed: 'right',width: 180}
        ]];
    };

    // 单击行事件
    table.on('row(' + PandianJihua.tableId + ')', function (obj) {
        var data = obj.data;
		$('div[lay-id="' + PandianJihua.tableId + '"]').find(".layui-table-body tr ").attr({ "style": "background:#FFFFFF" });//其他tr恢复原样
		$('div[lay-id="' + PandianJihua.tableId + '"]').find(obj.tr.selector).attr({ "style": "background:#e6e6e6" });//改变当前tr颜色
        pandianJihuaUser(data);
    });

	function pandianJihuaUser(data){
    	var diskId;
		var warehouseId;
    	if (data!=null) {
    		diskId = data.id;
			warehouseId = data.warehouseId;
		} else {
			diskId = 'nodata';
		}
        // 渲染商品明细表格
        var tableResult = table.render({
            elem: '#' + PandianJihuaUser.tableId,
            url: Feng.ctxPath + '/pandianJihuaUser/list?diskId=' + diskId,
            page: true,
            limit: 20,
            height: 380,
            toolbar: '#toolbarfromDetailed',
            cellMinWidth: 100,
            totalRow: true ,//开启合计行
            cols: PandianJihuaUser.initColumn()
        });

        // 添加按钮点击事件
        $('#btnAddDetailed').click(function () {
		    layer.open({
		        type: 2,
		        title: '人员安排',
		        area: ['800px', '550px'],
		        anim: 5,
		        content: Feng.ctxPath + '/pandianJihuaUser/add?warehouseId=' + warehouseId +'&diskId=' + diskId,
	            end: function () {
                	pandianJihuaUser(data);
	            }
		    });
        });

        // 工具条点击事件
        table.on('tool(' + PandianJihuaUser.tableId + ')', function (obj) {
            var data2 = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'deleteDetailed') {
                PandianJihuaUser.onDeleteItem(data2);
            }else if (layEvent === 'todetailed') {
                PandianJihuaUser.onTodetailed(data2);
            }
        });

        PandianJihuaUser.onTodetailed = function (data2) {
		    layer.open({
		        type: 2,
		        title: '盘点明细',
		        area: ['1050px', '600px'],
		        anim: 5,
		        content: Feng.ctxPath + '/pandianJihuaDetailed?areaId=' + data2.areaId +'&diskId=' + data2.diskId +'&createUser=' + data2.userId,
	            end: function () {
	            }
		    });
        };

        // 删除按钮点击事件
        PandianJihuaUser.onDeleteItem = function (data2) {
            var operation = function () {
                var ajax = new $ax(Feng.ctxPath + "/pandianJihuaUser/delete", function (data2) {
		        	if (data2.success) {
		                Feng.success("删除成功!");
                		pandianJihuaUser(data);
					} else {
			            Feng.error("删除失败！" + data2.message)
					}
                }, function (data2) {
                    Feng.error("删除失败!" + data2.responseJSON.message + "!");
                });
                ajax.set("id", data2.id);
                ajax.start();
            };
            Feng.confirm("是否删除?", operation);
        };
	}
});
